package com.market.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.market.domain.Order;
import com.market.domain.OrderDetail;
import com.market.domain.Product;
import com.market.domain.Purchase;
import com.market.domain.PurchaseDetail;
import com.market.manager.PurchaseManager;
import com.market.util.CollectionUtil;
import com.market.web.model.ProductModel;
import com.market.web.model.PurchaseModel;
import com.market.web.model.SupplierModel;

@Controller("purchaseController")
@Scope("session")
public class PurchaseController extends
		GenericController<PurchaseManager, PurchaseModel, Purchase> {

	@Autowired
	private SupplierModel supplierModel;
	
	@Autowired
	private ProductModel productModel;

	@Autowired
	@Override
	protected void setModel(PurchaseModel model) {
		this.model = model;

	}
	
	public Purchase confirmCurrent(){
		return this.manager.confirm(getModelCurrent());
	}
	
	public Purchase unConfirmCurrent(){
		return this.manager.unConfirm(getModelCurrent());
	}
	
	public Purchase receiveCurrent(){
		return this.manager.receive(getModelCurrent());
	}
	
	public Purchase unReceiveCurrent(){
		return this.manager.unReceive(getModelCurrent());
	}
	
	
	public Purchase processCurrent(){
		return this.manager.process(getModelCurrent());
	}
	
	public Purchase unProcessCurrent(){
		return this.manager.unProcess(getModelCurrent());
	}
	
	public Purchase payCurrent(){
		return this.manager.pay(getModelCurrent());
	}
	
	public Purchase cancelCurrent(){
		return this.manager.cancel(getModelCurrent());
	}

	@Autowired
	@Override
	protected void setManager(PurchaseManager manager) {
		this.manager = manager;
	}
	
	/**
	 * This method does nothing, just for setRemoveDetailFromCurrent work
	 * properly.
	 * 
	 * @param detail
	 */

	public void removeDetailFromCurrent() {

	}

	/**
	 * This method remove specific purchase detail from order.
	 * 
	 * @param detail
	 */
	public void setRemoveDetailFromCurrent(PurchaseDetail detail) {
		for (int i = 0; i < this.getModelCurrent().getPurchaseDetails().size(); i++) {
			if (this.getModelCurrent().getPurchaseDetails().get(i).getProduct()
					.equalsId(detail.getProduct())) {
				this.getModelCurrent().getPurchaseDetails().remove(i);
			}
		}
	}
	
	
	public void addSelectedProductToCurrent() {
		Purchase current = this.getModelCurrent();
		List<Product> selectedProducts = productModel.getSelecteds();

		List<PurchaseDetail> purchaseDetails = current.getPurchaseDetails();
		boolean exist = false;
		for (Product product : selectedProducts) {
			exist = false;
			for (PurchaseDetail purchaseDetail : purchaseDetails) {
				if (product.equalsId(purchaseDetail.getProduct())) {
					exist = true;
					break;
				}
			}
			if (exist == false) {
				current.getPurchaseDetails().add(
						this.model.createPurchaseDetail(product));
			}
		}
		CollectionUtil<Product> util = new CollectionUtil<Product>();
		util.clearSelection(selectedProducts);
	}

	public void updateCurrentSupplier() {
		this.model.getCurrent().setSupplier(this.supplierModel.getCurrent());
	}
}
